<?php

/**
 * login actions.
 *
 * @package    Redsea20
 * @subpackage login
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class loginActions extends sfActions
{
    /**
     * Execute Logout Process
     *
     * @param sfWebrequest $request  A request object
     */
    public function executeLogout(sfWebrequest $request)
    {
        $this->getUser()->setAuthenticated(false);
        $this->getUser()->shutdown();
        $this->redirect('main/index');
    }
 /**
  * Executes index action
  *
  * @param sfRequest $request A request object
  */
  public function executeIndex(sfWebRequest $request)
  {
    //$this->forward('default', 'module');
      //$sysDate = getdate();
      //$this->hour = sfConfig::get('sf_upload_dir');
      //$this->getUser()->setFlash('message','SUCCESSFUL');

      //$pwd = md5('ALI');
      //$dPwd = hash('MD5', 'ALI');
      //echo $pwd.'<BR/>'.$dPwd;

        if ( $this->getRequestParameter('Email') != null && $this->getRequestParameter('Password') != null )
        {
            $userDeail = Doctrine_Query::create()
                        ->select('l.id, l.entityid, l.password')
                        ->from('login l')
                        ->leftJoin('l.Entity r')
                        ->where('l.username=\''.$this->getRequestParameter('Email').'\'')
                        ->andwhere('l.password=\''.md5($this->getRequestParameter('Password')).'\'')
                        ->execute();

            if( $userDeail->count() == 1)
            {
                foreach($userDeail as $userInfo)
                {
                    //$this->getUser()->setFlash('msg', 'Login Successful.');
                    $this->getUser()->setAuthenticated(true);
                    $this->getUser()->setAttribute("logged", true);
                    $this->getUser()->setAttribute("currentuser", Entity::GetEntity($userInfo->getEntityid("id")));
                    //$this->getUser()->setAttribute("username", $userInfo->Entity["firstname"].' '.$userInfo->Entity["lastname"]);
                    //$this->getUser()->setAttribute("userid", $userInfo->getEntityid("id"));

                    // Forward to HOME PAGE
                    $this->redirect('home/index');  // module + '/' + action (indexSuccess.php)
                }
                //echo $userDeail[0]->Entity["firstname"].' '.$userDeail[0]->Entity["lastname"];
            }
            else
            {
                $this->getUser()->setFlash('msg', 'Invalid Username/Password. Try Again!.');
                //$this->getRequest()->setError('username', 'Username cannot be blank');
                $this->redirect('main/index');  // module + '/' + action (indexSuccess.php)
            }
        }
        else
        {
            $this->getUser()->setFlash('msg', 'Invalid Username/Password. Try Again!.');
            //$this->getRequest()->setError('username', 'Username cannot be blank');
            $this->redirect('main/index');  // module + '/' + action (indexSuccess.php)
            //echo 'Invalid Username/Password';

        }

//      foreach($this->q as $item){
//        echo $item->getPassword();
//      }
  }

  public function  executeLogin()
  {
    sView::SUCCESS;
  }

  public function executePrepareLogin()
  {
    if($this->getRequest()->getMethid() != sfRequest::POST)
    {
        sView::SUCCESS;
    }
    else
    {
        $usr = $this->getRequestParameter('username');
        $pwd  =  $this->getRequestParameter('password');

        //$pwd = md5($pwd);

        if($usr == "test" && $pwd == "test")
        {
            echo 'LOGIN SUCCESS';
        }
        else
        {

            echo 'LOGIN FAIL';
        }
    }
  }
}
